package com.jpa.model;

import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;

import com.vaadin.ui.Table;

public class Detail {
	private Table table;
	private int month;
	private boolean isIncome;
	public ResourceBundle Messages;

	public Detail(Table table, int month, boolean isIncome,
			ResourceBundle Messages) {
		this.table = table;
		this.month = month;
		this.isIncome = isIncome;
		this.Messages = Messages;
	}

	public void showDetail(Table detailTable) {
		int a;
		detailTable.removeAllItems();
		Iterator<?> it = table.getItemIds().iterator();
		while (it.hasNext()) {
			a = (Integer) it.next();
			if ((Integer) table.getContainerProperty(a, "month").getValue() == month) { //$NON-NLS-1$
				String type;
				int in = 0;
				int inwo = 0;
				type = (String) table.getContainerProperty(a, "type") //$NON-NLS-1$
						.getValue();
				in = (Integer) table.getContainerProperty(a, "total") //$NON-NLS-1$
						.getValue();
				inwo = (Integer) table.getContainerProperty(a, "price") //$NON-NLS-1$
						.getValue();

				detailTable.addItem(new Object[] { type, in, inwo }, null);

			}
		}
		Iterator<?> itr = detailTable.getItemIds().iterator();
		int b;
		int t = 0;
		int y = 0;
		if (isIncome) {
			while (itr.hasNext()) {
				b = (Integer) itr.next();
				t += (Integer) detailTable.getContainerProperty(b,
						Messages.getString("Detail.in")) //$NON-NLS-1$
						.getValue();
				y += (Integer) detailTable.getContainerProperty(b,
						Messages.getString("Detail.inwovat")).getValue(); //$NON-NLS-1$
			}
			detailTable.setFooterVisible(true);
			detailTable
					.setColumnFooter(
							Messages.getString("Detail.type"), Messages.getString("Detail.total")); //$NON-NLS-1$ //$NON-NLS-2$
			detailTable.setColumnFooter(Messages.getString("Detail.in"), //$NON-NLS-1$
					NumberFormat.getNumberInstance(Locale.US).format(t));
			detailTable.setColumnFooter(
					Messages.getString("Detail.inwovat"), NumberFormat //$NON-NLS-1$
							.getNumberInstance(Locale.US).format(y));
		} else {
			while (itr.hasNext()) {
				b = (Integer) itr.next();
				t += (Integer) detailTable.getContainerProperty(b,
						Messages.getString("Detail.out")) //$NON-NLS-1$
						.getValue();
				y += (Integer) detailTable.getContainerProperty(b,
						Messages.getString("Detail.outwovat")).getValue(); //$NON-NLS-1$
			}
			detailTable.setFooterVisible(true);
			detailTable
					.setColumnFooter(
							Messages.getString("Detail.type"), Messages.getString("Detail.total")); //$NON-NLS-1$ //$NON-NLS-2$
			detailTable.setColumnFooter(Messages.getString("Detail.out"), //$NON-NLS-1$
					NumberFormat.getNumberInstance(Locale.US).format(t));
			detailTable.setColumnFooter(
					Messages.getString("Detail.outwovat"), NumberFormat //$NON-NLS-1$
							.getNumberInstance(Locale.US).format(t));
		}

		detailTable.setPageLength(detailTable.size());
	}

}
